package com.example.jiemian;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.example.jiemian.pojo.ceshiModel;

import java.util.ArrayList;
import java.util.List;

public class ceshi3 extends Fragment {




    //声明一个表格对象
    private GridView gridView;
    //声明一个图片集合
    private  ArrayList<ceshiModel> list=new ArrayList<>();
    //图片数组集合
    private int[] image={R.mipmap.baojingqi,R.mipmap.choushuibeng,R.mipmap.tongfengji,R.mipmap.zhaomingdeng};


    //声明一个适配器
    myAdapter myAdapter;

    @Override
    public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle){
        //获取图片显示主框架对象
        View view=layoutInflater.inflate(R.layout.ceshi3,gridView,false);

        //获取主矿框架中表格控件
        gridView=(GridView) view.findViewById(R.id.ceshi3_gridview_1);
        //将数据放到几何中
        list.clear();
        list.add(new ceshiModel("通风机:","开","运行时长"));
        list.add(new ceshiModel("照明灯:","开"," "));
        list.add(new ceshiModel("抽水机:","开","运行时长"));
        list.add(new ceshiModel("报警器:","开","运行时长"));

        myAdapter=new myAdapter(image);

        gridView.setAdapter(myAdapter);



        return view;
    }


    public class myAdapter extends BaseAdapter{

        private int[] image1;
        //通过构造函数，将外界的值与内部变量进行交互
        public myAdapter(int[] image) {
            this.image1 = image;
        }


        @Override
        public int getCount() {
            return this.image1.length;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        //实现模板页的状态加载
        @SuppressLint("SetTextI18n")
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {


            //通过模板实例化控件
            @SuppressLint({"ViewHolder", "InflateParams"})
            View view=LayoutInflater.from(getActivity()).inflate(R.layout.ceshi3_item_view,null);
            //通过模板对象，获取模板空间
            final TextView textView1=(TextView) view.findViewById(R.id.ceshi3_textview_1);
            final TextView textView2=(TextView) view.findViewById(R.id.ceshi3_textview_2);

            ImageView imageView1=(ImageView)view.findViewById(R.id.ceshi3_imageview_1);
            ToggleButton toggleButton1=(ToggleButton)view.findViewById(R.id.ceshi3_togbtn);

            //toggleButton1.setOnClickListener(toggleButton1.isChecked());//监听



            //从几何中获取数据并加载到对应控件上
            final String name=(String) list.get(position).getImage_name();
            String title=(String) list.get(position).getImage_title();





            textView1.setText(""+name+"开启");
            textView2.setText(""+title);
            imageView1.setImageResource(image1[position]);
            toggleButton1.setChecked(true);

            toggleButton1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if(isChecked){
                        textView1.setText(name+"开启");
                    }else{
                        textView1.setText(name+"关闭");
                    }
                }
            });


            return view;
        }


    }


}
